<?php
require 'admin'.EXT;
class Customer extends Admin{

	function __construct()
	{
		parent::__construct();
		$this->load->model("customer_model");
		$this->load->model("new_order_model");
		$this->load->model("all_order_model");
		$this->load->model("my_order_model");
		$this->load->library('Pagination');
		$this->load->helper("datetime");
		$this->data['pay_status'] = array('Under Payment','Paid','Finish Booked','Pending Payment','Refund');
	}
	
	function index()
	{
		admin_redirect('customer/all_order');
	}
	
	function check_permission()
	{
		$group = $_SESSION['_admin']['group_id'];
		switch($group)
		{
			case 1:
				$permission = array('Under Payment','Pending Payment','Paid','Refund','Finish Booked');
				break;
			case 3:
				$permission = array('Under Payment','Pending Payment');
				break;
			case 4:
				$permission = array('Paid','Refund','Finish Booked');
				break;
			case 5:
				$permission = array('Finish Booked');
				break;
		}
		return $permission;
	}
	
	function all_order()
	{
		/**
		 * Paging
		 */
		$key 						= isset($_GET['key']) ? $_GET['key'] : "";
		$field						= isset($_GET['field']) ? $_GET['field'] : "";
		if($key != "" && $field != "")
		{
			$where = array($field=>$key);
			$url   = admin_url("all_order?key=".$key."&field=".$field);
		}
		else
		{
			$where = array();
			$url					= admin_url('customer/all_order');
		}
		$pay_status					= @$_GET['pay_stt'];
		$pay_status					= preg_replace('/_/', ' ', $pay_status);
		if($pay_status == "")
		{
			$total_row			 	= $this->all_order_model->total_all_order($where);
			$url					= admin_url('customer/all_order');
		}
		else
		{
			$total_row			 	= $this->all_order_model->total_order_paystatus(array("pay_status"=>$pay_status));
			$url					= admin_url("customer/all_order?pay_stt=".$pay_status);
		}
		$config['total_rows'] 		= $total_row["total"];
    	$page						= (isset($_GET['p']) ? $_GET['p'] : 1);
    	$limit						= 20;
    	$start						= ($page-1)*$limit;
    	$conf	=	array(
    		'base_url'		=>	$url,
    		'total_rows'	=>	$total_row,
    		'per_page'		=>	$limit,
    		'cur_page'		=>	$page
    	);
    	$this->pagination->initialize($conf);
    	$this->data['pagnav']		=	$this->pagination->display_query_string();
		/**
		 * Data and values
		 */
    	$this->data["field_search"]	= array(
    									"customers.email"=>"Customer Email",
    									"customers.last_name"=>"Customer Name"
    								  );
		$this->data["field"]		= $field;
    	$this->data["keyword"]		= $key ? $key : "Keyword";
		$this->data["total_order"]	= $total_row;
		$this->data["under_payment"]= $this->all_order_model->total_order_paystatus(array("pay_status"=>"Under Payment"));
		$this->data["paid"]			= $this->all_order_model->total_order_paystatus(array("pay_status"=>"Paid"));
		$this->data["finish_booked"]= $this->all_order_model->total_order_paystatus(array("pay_status"=>"Finish Booked"));
		$this->data["pending_ref"] 	= $this->all_order_model->total_order_paystatus(array("pay_status"=>"Pending Payment"));
		$this->data["refund"] 		= $this->all_order_model->total_order_paystatus(array("pay_status"=>"Refund"));
		$this->data['pay_status']	= array('Under Payment','Paid','Finish-Booked','Pending Refund','Refund');
		$this->data['page']		   	= $this->pagination->create_links();
		$this->data['list_order']	= $this->all_order_model->list_all_order($start, $limit, $pay_status, $where);
		$this->data["tpl_file"] 	= "customer/all_order";		
		/**
		 * View
		 */
		$this->load->view("layout/default", $this->data);
	}
	
	function new_order()
	{
		/**
		 * Paging
		 */
		$this->data['total_order']  = $this->new_order_model->get_total_new_order();
    	$page						= (isset($_GET['p']) ? $_GET['p'] : 1);
    	$limit						= 20;
    	$start						= ($page-1)*$limit;
    	$url						= admin_url('customer/new_order');
    	$conf	=	array(
    		'base_url'		=>	$url,
    		'total_rows'	=>	$this->data['total_order'],
    		'per_page'		=>	$limit,
    		'cur_page'		=>	$page
    	);
    	$this->pagination->initialize($conf);
    	$this->data['pagnav']		=	$this->pagination->display_query_string(); 
    	/**
    	 * Submit form
    	 */   	
    	if($_SERVER['REQUEST_METHOD'] == 'POST')
    	{
    		$this->form_validation->set_message('required','You must select one order!');
    		$this->form_validation->set_rules("order_id", "", "required");
			if($this->form_validation->run() == TRUE)
			{
				$order_id = $this->input->post("order_id");	
				$user_process = $_SESSION['_admin']['username'];
				$data = array("user_process"=>$user_process);
				$this->new_order_model->update_user_process($order_id, $data);
				admin_redirect("customer/new_order");
			}
			else
			{
				$this->message->error = $this->form_validation->get_error_array();
			}
    	}
		
		/**
		 * Data and values
		 */
    	$this->data['list_order'] 	= $this->new_order_model->list_new_order($start, $limit);
		$this->data["tpl_file"]  	= "customer/new_order";
		/**
		 * View
		 */
		$this->load->view("layout/default", $this->data);
	}

	function customers()
	{
		$this->data['total_order']  = $this->customer_model->total_all_customer();
    	$page						= (isset($_GET['p']) ? $_GET['p'] : 1);
    	$limit						= 20;
    	$start						= ($page-1)*$limit;
    	$url						= admin_url('customer');
    	$conf	=	array(
    		'base_url'		=>	$url,
    		'total_rows'	=>	$this->data['total_order'],
    		'per_page'		=>	$limit,
    		'cur_page'		=>	$page
    	);
    	$this->pagination->initialize($conf);
    	$this->data['pagnav']		=	$this->pagination->display_query_string(); 
    	/**
    	 * Data and value
    	 */
    	$this->data["total_cus"] 	= $this->customer_model->total_all_customer();
    	$this->data["total_activ"]	= $this->customer_model->total_active_customer();
    	$this->data["list_customer"]= $this->customer_model->list_all_customer($start, $limit);
		if($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			$cus_id = $this->input->post('cus_id');
			$this->form_validation->set_message('required','You must enter a value %s!');
			$this->form_validation->set_message('valid_email',' Invalid email!');
    		$this->form_validation->set_rules("title", "First name", "required");
    		$this->form_validation->set_rules("fullname", "Last name", "required");
    		$this->form_validation->set_rules("email", "", "required|valid_email");
    		if($this->form_validation->run() == TRUE)
    		{
    			$data = array(
    				"title"=>$this->input->post('title'),
    				"fullname"=>$this->input->post('fullname'),
    				"email"=>$this->input->post('email'),
    				"address"=>$this->input->post('address'),
    				"telephone"=>$this->input->post('telephone'),
    				"fax"=>$this->input->post('fax'),
    				"cellphone"=>$this->input->post('cellphone'),
    				"last_update"=>date('Y-m-d H:i:s', time()),
    				"country"=>$this->input->post('country'),
    				"active"=>$this->input->post('active') ? $this->input->post('active') : "no"
    			);
    			$this->customer_model->customer_update($cus_id, $data);
    			admin_redirect('customer/customers');
    		}
    		else
    		{
    			$this->message->error = $this->form_validation->get_error_array();
    		}
		}
		else
		{
			if(@$_GET['del'] == "yes")
			{
				$cus_id = @$_GET['cus_id'];
				$this->customer_model->customer_delete($cus_id);
				admin_redirect("customer/customers");
			}
		}
    	/**
    	 * View
    	 */
    	$this->data["tpl_file"]		=	"customer/customers";
    	$this->load->view("layout/default", $this->data);
	}
	
	function customers_create()
	{
		if($_SERVER["REQUEST_METHOD"] == "POST")
		{
			$this->form_validation->set_message('required','You must enter a value %s!');
			$this->form_validation->set_message('valid_email',' Invalid email!');
			$this->form_validation->set_rules("title", "title", "required");
    		$this->form_validation->set_rules("fullname", "fullname", "required");
    		$this->form_validation->set_rules("email", "email", "required|valid_email");
    		$this->form_validation->set_rules("address", "address", "required");
    		$this->form_validation->set_rules("telephone", "telephone", "required");
    		$this->form_validation->set_rules("fax", "fax", "required");
    		$this->form_validation->set_rules("cell_phone", "cell phone", "required");
    		if($this->form_validation->run() == TRUE)
    		{
    			$data = array(
    				"title"=>$this->input->post('title'),
    				"fullname"=>trim($this->input->post('fullname')),
    				"email"=>$this->input->post('email'),
    				"address"=>$this->input->post('address'),
    				"telephone"=>$this->input->post('telephone'),
    				"fax"=>$this->input->post('fax'),
    				"cellphone"=>$this->input->post('cell_phone'),
    				"date_create"=>date('Y-m-d H:i:s', time()),
    				"last_update"=>date('Y-m-d H:i:s', time()),
    				"cellphone"=>$this->input->post('cell_phone'),
    				"country"=>$this->input->post('country'),
    				"last_ip"=>$_SERVER['REMOTE_ADDR'],
    				"active"=>$this->input->post('active') ? $this->input->post('active') : "no"
    			);
    			if($this->customer_model->check_customer(trim($data['email'])) == false)
    			{
	    			$this->customer_model->customer_create($data);    	
	    			admin_redirect("customer/customers");	
    			}
    			else
    			{
    				$this->message->add('error', "This customer was existed.");
    			}	
    		}
    		else
    		{
    			$this->message->error = $this->form_validation->get_error_array();
    		}
		}
		else
		{
			$this->message->add('error','Error!');
		}
		$this->data['tpl_file'] = "customer/my_customer_create";
		$this->load->view("layout/default", $this->data);
	}
		
	function customers_edit()
	{
		$customer_id = $_GET['cus_id'];
		$customer_vew = @$_GET['view'];
		$this->data['readonly'] = ($customer_vew == "yes") ? "disabled=\"disabled\"" : "";
		$this->data["customer"] = $this->customer_model->get_customer_by_id($customer_id);
		$this->data['tpl_file'] = "customer/edit_cus";
		$this->load->view($this->data['tpl_file'], $this->data);
	}
	
	
	
	function my_order()
	{
		$pay_status = preg_replace('/_/', ' ',  @$_GET['pay_stt']);
		/**
		 * Paging
		 */
		$page						= (isset($_GET['p']) ? $_GET['p'] : 1);
    	$limit						= 20;
    	$start						= ($page-1)*$limit;
    	$url						= admin_url('customer/my_order');
    	$author_name 				= $_SESSION['_admin']['username'];
    	$this->data['total_myorder']= $this->my_order_model->total_my_order($author_name);
    	$conf	=	array(
    		'base_url'		=>	$url,
    		'total_rows'	=>	$this->data['total_myorder'],
    		'per_page'		=>	$limit,
    		'cur_page'		=>	$page
    	);
    	$this->pagination->initialize($conf);
    	$this->data['total_order']  = $this->data['total_myorder'];
    	$this->data['pagnav']   	= $this->pagination->display_query_string();
    	$this->data['list_order']	= $this->my_order_model->get_list_myorder($start, $limit, $pay_status, $whe=array(), $author_name);
    	
    	$per = $this->check_permission();
    	if($pay_status != "")
    	{
	    	if(!in_array($pay_status, $per)) 
	    	{
	    		admin_redirect('customer/my_order');
	    		exit();
	    	}
    	}
    	foreach($per as $item)
    	{
    		$this->data['per'][$item] = $this->my_order_model->total_myorder_paystatus(array("pay_status"=>$item), $author_name);
    	}
    	/*
    	 * POST
    	 */
    	if($_SERVER["REQUEST_METHOD"] == "POST")
    	{
    		$action = $_POST['act'];
    		switch($action)
    		{
    			case 1:
    				{
    					$j = 0;
    					foreach($_POST['o_id'] as $key=>$value)
    					{
    						$data = array('pay_status'=>$_POST['pay_status'][$j]);
    						$this->my_order_model->my_order_update_pay_status($value, $data, $_SESSION['_admin']['username']);
    						$j++;
    					}
    					admin_redirect('customer/my_order');
    				}
    				break;
    			case 2:
    				{
    					foreach($_POST['order_id'] as $item)
    					{
    						$data = array('is_del'=>'yes');
    						$this->my_order_model->my_order_del($item, $data);
    					}
    					admin_redirect('customer/my_order');
    				}
    				break;
    		}
    	}
    	/**
    	 * View
    	 */
    	$this->data['tpl_file'] = "customer/my_order";
    	$this->load->view("layout/default", $this->data);
	}
	
	function my_order_view()
	{
		$order_id 				= @$_GET["order_id"];
		$customer_vew 			= @$_GET['view'];
		$this->data['readonly'] = ($customer_vew == "yes") ? "disabled=\"disabled\"" : "";
		$author_name 			= $_SESSION['_admin']['username'];
		$this->data["order"] 	= $this->my_order_model->get_my_order_id($order_id, $author_name);
		/**
    	 * View
    	 */
    	$this->data['tpl_file'] = "customer/my_order_view";
    	$this->load->view($this->data['tpl_file'], $this->data);
	}
	
	function my_order_create()
	{
		$op = @$_GET['op'];
		if($op == "new_customer")
		{
			$this->data['tpl_file'] = "customer/my_customer_create";
		}
		else
		{
			$this->data['customer'] 	= $this->my_order_model->get_customer();
			$this->data['tour_code'] 	= $this->my_order_model->get_tour_code();
			$this->data['pay_status']	= array('Under Payment','Paid','Finish-Booked','Pending Refund','Refund');
			$this->data['room_preferred'] = array('Single','Double','Twin','Triple');
			$this->data['hotel_preferred'] = array('Standard','Deluxe');
			$this->data['tpl_file'] 	= "customer/my_order_create";
			if($_SERVER["REQUEST_METHOD"] == "POST")
			{
				$this->form_validation->set_message('required','You must enter a value %s!');
				$this->form_validation->set_message('valid_email',' Invalid email!');
				$this->form_validation->set_rules("num_persons", "num persons", "required");
	    		$this->form_validation->set_rules("total_price", "total price", "required");
	    		if($this->form_validation->run() == TRUE)
	    		{
	    			$data_order = array(
	    				"customer_id"=>$this->input->post('customer_id'),
	    				"date_create"=>date('Y-m-d H:i:s', time()),
	    				"user_process"=>$_SESSION['_admin']['username'],
	    				"date_process"=>date('Y-m-d H:i:s', time())
	    			);
	    			$data_session = array(
	    				"tour_id"=>$this->input->post('tour_id'),
	    				"date_departure"=>$this->input->post('date_departure'),
	    				"end_date"=>$this->input->post('end_date'),
	    				"num_persons"=>$this->input->post('num_persons'),
	    				"num_adult"=>$this->input->post('num_adult'),
	    				"num_chil"=>$this->input->post('num_chil'),
	    				"num_baby"=>$this->input->post('num_baby'),
	    				"detail_persons"=>$this->input->post('detail_persons'),
	    				"total_price"=>$this->input->post('total_price'),
	    				"num_rooms"=>$this->input->post('num_rooms'),
	    				"room_preferred"=>$this->input->post('room_preferred'),
	    				"hotel_preferred"=>$this->input->post('hotel_preferred'),
	    				"request_for_visa"=>$this->input->post('request_for_visa') ? $this->input->post('request_for_visa') : "no",
	    				"request_hotel"=>$this->input->post('request_hotel') ? $this->input->post('request_hotel') : "no",
	    				"flight_number"=>$this->input->post('flight_number'),
	    				"date_arrival"=>$this->input->post('date_arrival'),
	    				"time_arrival"=>$this->input->post('time_arrival'),
	    				"order_info"=>$this->input->post('order_info'),
	    				"contact_status"=>$this->input->post('contact_status') ? $this->input->post('contact_status') : "no",
	    				"pro_code"=>$this->input->post('pro_code'),
	    				"total_pay"=>$this->input->post('total_pay'),
	    				"guest_name"=>$this->input->post('guest_name'),
	    				"user_create"=>$_SESSION['_admin']['username'], 
	    				"pay_status"=>$this->input->post('pay_stt'), 	
	    				"ip_address"=>$_SERVER['REMOTE_ADDR']			
	    			);
	    			$this->my_order_model->my_order_insert($data_order, $data_session); 
	    			admin_redirect("customer/my_order");		
	    		}
	    		else
	    		{
	    			$this->message->error = $this->form_validation->get_error_array();
	    		}
			}
		}
		
		$this->load->view("layout/default", $this->data);
	}
	
	function my_order_edit()
	{
		$session_id = @$_GET['order_id'];
		$this->data['customer'] 	= $this->my_order_model->get_customer();
		$this->data['tour_code'] 	= $this->my_order_model->get_tour_code();
		
		$this->data['room_preferred'] = array('Single','Double','Twin','Triple');
		$this->data['hotel_preferred'] = array('Standard','Deluxe');
		$this->data['order']		= $this->my_order_model->get_my_order_id($session_id, $_SESSION['_admin']['username']);
		if(count($this->data['order']) > 0)
		{
			$this->data['tpl_file'] 	= "customer/my_order_edit";
			if($_SERVER["REQUEST_METHOD"] == "POST")
			{
				$this->form_validation->set_message('required','You must enter a value %s!');
				$this->form_validation->set_message('valid_email',' Invalid email!');
				$this->form_validation->set_rules("num_persons", "num persons", "required");
	    		$this->form_validation->set_rules("total_price", "total price", "required");
	    		if($this->form_validation->run() == TRUE)
	    		{
	    			$data_order = array(
	    				"customer_id"=>$this->input->post('customer_id'),
	    				"date_create"=>date('Y-m-d H:i:s', time()),
	    				"user_process"=>$_SESSION['_admin']['username'],
	    				"date_process"=>date('Y-m-d H:i:s', time())
	    			);
	    			$data_session = array(
	    				"tour_id"=>$this->input->post('tour_id'),
	    				"date_departure"=>$this->input->post('date_departure'),
	    				"end_date"=>$this->input->post('end_date'),
	    				"num_persons"=>$this->input->post('num_persons'),
	    				"num_adult"=>$this->input->post('num_adult'),
	    				"num_chil"=>$this->input->post('num_chil'),
	    				"num_baby"=>$this->input->post('num_baby'),
	    				"detail_persons"=>$this->input->post('detail_persons'),
	    				"total_price"=>$this->input->post('total_price'),
	    				"num_rooms"=>$this->input->post('num_rooms'),
	    				"room_preferred"=>$this->input->post('room_preferred'),
	    				"hotel_preferred"=>$this->input->post('hotel_preferred'),
	    				"request_for_visa"=>$this->input->post('request_for_visa') ? $this->input->post('request_for_visa') : "no",
	    				"request_hotel"=>$this->input->post('request_hotel') ? $this->input->post('request_hotel') : "no",
	    				"flight_number"=>$this->input->post('flight_number'),
	    				"date_arrival"=>$this->input->post('date_arrival'),
	    				"time_arrival"=>$this->input->post('time_arrival'),
	    				"order_info"=>$this->input->post('order_info'),
	    				"contact_status"=>$this->input->post('contact_status') ? $this->input->post('contact_status') : "no",
	    				"pro_code"=>$this->input->post('pro_code'),
	    				"total_pay"=>$this->input->post('total_pay'),
	    				"guest_name"=>$this->input->post('guest_name'),
	    				"user_create"=>$_SESSION['_admin']['username'], 
	    				"pay_status"=>$this->input->post('pay_stt'), 				
	    			);
	    			$this->my_order_model->my_order_update_tbl_orders($this->data['order']['id'], $data_order); 
	    			$this->my_order_model->my_order_update_tbl_sessions($session_id, $data_session);    	
	    			admin_redirect("customer/my_order");		
	    		}
	    		else
	    		{
	    			admin_redirect("customer/my_order");
	    		}
			}
			$this->load->view("layout/default", $this->data);
		} 
		else
		{
			$this->message->add('error', 'You can\'t edit this order!');
			$this->data['tpl_file'] 	= "home/index";
			$this->load->view("layout/default", $this->data);
		}
	}
	
	function my_order_del()
	{
		$order_id = @$_GET['order_id'];
		$data = array("is_del"=>"yes");
		$this->my_order_model->my_order_del($order_id, $data);
		admin_redirect("customer/my_order");
	}
	
	function add_note()
	{	
		
		$this->data['order_id'] = @$_GET['order_id']; 
		if($_SERVER["REQUEST_METHOD"] == "POST")
		{
			$this->form_validation->set_message('required','You must enter a value %s!');
			$this->form_validation->set_rules("msg", "message", "required");
			if($this->form_validation->run() == TRUE)
			{
				$data = array(
					'order_id'=>$this->input->post('order_id'),
					'user_note'=>$_SESSION['_admin']['username'],
					'massege'=>$this->input->post('msg'),
					'time_note'=>date('Y-m-d H:i:s', time())
				);
				$this->my_order_model->my_order_add_note($data);
				admin_redirect("customer/my_order");
			}
			else
			{
				$this->message->error = $this->form_validation->get_error_array();
				
			}
		}
		$this->load->view('customer/my_order_note', $this->data);
	}
}